package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//需要设计接口错误码
type Response struct {
	// API 状态码;
	ApiCode string      `json:"code"`
	Data    interface{} `json:"data"`
	ApiMsg  string      `json:"msg"`
}

const (
	DEFAULT = "000"
	SUCCESS = "001"
	FAILED  = "002"
)

var codeMsgMap = map[string]string{
	DEFAULT: "common response",
	SUCCESS: "request success",
	FAILED:  " request failed",
}

func Result(httpStatus int, resp *Response, c *gin.Context) {
	c.JSON(httpStatus, resp)
}

func Ok(c *gin.Context) {
	Result(http.StatusOK, &Response{
		ApiCode: SUCCESS,
		Data:    map[string]interface{}{},
		ApiMsg:  codeMsgMap[SUCCESS],
	}, c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(http.StatusOK, &Response{
		ApiCode: SUCCESS,
		Data:    map[string]interface{}{},
		ApiMsg:  message,
	}, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(http.StatusOK, &Response{
		ApiCode: SUCCESS,
		Data:    data,
		ApiMsg:  codeMsgMap[SUCCESS],
	}, c)
}

func OkWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(http.StatusOK, &Response{
		ApiCode: SUCCESS,
		Data:    data,
		ApiMsg:  message,
	}, c)

}

func Fail(c *gin.Context) {

	Result(http.StatusBadRequest, &Response{
		ApiCode: FAILED,
		Data:    map[string]interface{}{},
		ApiMsg:  codeMsgMap[FAILED],
	}, c)

}

func FailWithMessage(message string, c *gin.Context) {

	Result(http.StatusBadRequest, &Response{
		ApiCode: DEFAULT,
		Data:    map[string]interface{}{},
		ApiMsg:  message,
	}, c)
}

func FailWithDetailed(data interface{}, message string, c *gin.Context) {

	Result(http.StatusBadRequest, &Response{
		ApiCode: DEFAULT,
		Data:    data,
		ApiMsg:  message,
	}, c)
}
